from django.db import models

# Create your models here.

class Language(models.Model):
    """
    Data model for world languages
    """
    code                = models.CharField('ISO-639-1', maxlength = 2, blank = True)
    name_en				= models.CharField('Name (en)', maxlength = 50, help_text = 'max. 100 characters')
    name_es				= models.CharField('Name (es)', maxlength = 50, blank = True, help_text = 'Spanish translation (max. 50 characters)')
    name_fr				= models.CharField('Name (fr)', maxlength = 50, blank = True, help_text = 'French translation (max. 50 characters)')
    name_pt				= models.CharField('Name (pt)', maxlength = 50, blank = True, help_text = 'Portuguese translation (max. 50 characters)')
    
    
    class Meta:
        db_table             = 'world_languages'
        verbose_name         = 'language'
        verbose_name_plural  = 'languages'
        ordering             = ('name_en',)
        
    class Admin:
        list_display         = ('name_en', 'name_es', 'name_fr', 'name_pt', )
        search_fields        = ('name_en',)
        list_per_page        = 50
        fields = (
            ('Required Fields', {
            	'fields': ('code', 'name_en',)
            }),
            ('Translations', {
            	'fields': (
            		'name_es', 
            		'name_fr',
            		'name_pt',
            	), 
            	'classes': 'collapse',
            }),
        )
        

    def __unicode__(self):
        try:
        	name_field = "name_%s" % translation.get_language()
        	value = getattr(self, name_field)
        	if value:
        		return u'%s' % value
        	else:
        		return self.name_en
        except:
        	return self.name_en
        


class Country(models.Model):
    """
    Data model for world counties
    """
    code                     = models.CharField(maxlength = 3, unique = True)
    name_en					 = models.CharField('Name (en)', maxlength = 100, help_text = 'English name (max. 100 characters')
    name_es					 = models.CharField('Name (es)', maxlength = 100, blank = True, help_text = 'Spanish name (max. 100 characters)')
    name_fr					 = models.CharField('Name (fr)', maxlength = 100, blank = True, help_text = 'French name (max. 100 characters)')    
    name_pt					 = models.CharField('Name (pt)', maxlength = 100, blank = True, help_text = 'Portuguese name (max. 100 characters)')
    
    
    class Meta:
        db_table             = 'world_countries'
        verbose_name         = 'country'
        verbose_name_plural  = 'countries'
        ordering             = ('name_en',)
    

    class Admin:
        list_display         = ('name_en', 'code', 'name_es', 'name_fr', 'name_pt', )
        search_fields        = ('name_en',)
        list_per_page        = 50
        fields = (
            ('Required Fields', {
            	'fields': ('code', 'name_en',)
            }),
            ('Translations', {
            	'fields': ( 
            		'name_es', 
            		'name_fr',
            		'name_pt',
            	), 
            	'classes': 'collapse',
            }),
        )


    def __unicode__(self):
        try:
        	name_field = "name_%s" % translation.get_language()
        	value = getattr(self, name_field)
        	if value:
        		return u'%s' % value
        	else:
        		return self.name_en
        except:
        	return self.name_en

